package newx.com.control.comboBox
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextFieldAutoSize;
	
	import newx.StageInfo;
	import newx.com.NxUIComponent;
	import newx.com.control.IComboBox;
	import newx.com.control.list.NxList;
	import newx.com.control.list.NxListEvent;
	import newx.com.control.Scroll.NxScrollBar;
	
	/**
	 * 下拉菜单
	 * @author wangjun
	 * 
	 */	
	public class NxComboBox extends NxUIComponent implements IComboBox
	{
		private var _width : int = 78;
		
		private var _height : int = 21;
		
		private var _defaultTxtColor : uint = 0xFF0000;
		
		private var _skin : String = "Comb_label,TabelBGBtn_2,vScrol_1,vScrol_2";
		
		private var _titleSkin : String = "";
		
		private var _downBtnWidth : int = 18;
		
		private var _vScrollSkin	:String	;
		private var _hScrollSkin	:String	;
		private var _itemSkin		:String	= "";
		
		/**
		 *	对其方式 
		 */
		private var _align : String = TextFieldAutoSize.LEFT;
		
		/**
		 *	title容器 
		 */
		protected var _titleContainer:Sprite;
		/**
		 *	title项 
		 */
		protected var _titleItem:LabelItem;
		
		/**
		 *	下拉条容器 
		 */
		private var _listContainer:Sprite;
		/**
		 *	下拉条 
		 */
		protected var _nxList:NxList;
		
		protected var _dataList:Array = [];
		
		private var _showCount:int = 6;
		
		public function NxComboBox()
		{
			init();
		}
		
		private function init():void
		{
			drawContainer();
		}
		
		private function drawContainer():void
		{
			this.buttonMode = true;
			_titleContainer = new Sprite();
			this.addChild(_titleContainer);
			_listContainer = new Sprite();
			this.addChild(_listContainer);
		}
		
		protected function draw():void
		{
			if(_skin == "") return;
			//第一个逗号位置
			var index : int = _skin.indexOf(",");
			drawTitle(_skin.substring(0,index),_width,_height,_downBtnWidth);
			drawList(_skin.substring(index + 1,_skin.length),_width,_height);
			
			addListener();
			hideList();
		}
		
		private function drawList(skin : String, width : int, height : int):void
		{
			clearAll(_listContainer);
			
			_nxList = new NxList();
			var listSkin : Array = skin.split(",");
			_nxList.defaultTxtColor = _defaultTxtColor;
			_nxList.itemSkin = listSkin[0];
			_nxList.vScrollSkin = listSkin[1];
			_nxList.hScrollSkin = listSkin[2];
			_nxList.width = width;
			_nxList.height = height;
			_nxList.showCount = _showCount;
			_nxList.dataProvider = _dataList;
			_nxList.itemAlign = _align;
			_listContainer.addChild(_nxList);
		}
		
		private function drawTitle(skin : String, width : int, height : int, downBtnWidth : int):void
		{
			clearAll(_titleContainer);
			
			_titleItem = new LabelItem(skin,width,height,downBtnWidth);
			_titleItem.align = _align;
			_titleContainer.addChild(_titleItem);
			_listContainer.y = _titleItem.height;
		}
		
		private function initTitle($index:int):void
		{
			if($index >= _dataList.length) return;
			
			_titleItem.data = _dataList[$index];
		}
		
		public function set TitleSkin(titleSkin : String) : void
		{
			_titleSkin = titleSkin;
			
			_titleItem.skin = titleSkin;
		}
		
		public function get TitleSkin () : String
		{
			return _titleSkin;
		}
		
		public function get vScrollSkin():String
		{
			return _vScrollSkin;
		}
		
		public function set vScrollSkin(val:String):void
		{
			if(_vScrollSkin	!= val)
			{
				_vScrollSkin		=	val;
				if(null		!=		_nxList)	
				{
					_nxList.vScrollSkin = val;
				}
			}
		}
		
		public function get hScrollSkin():String
		{
			return _hScrollSkin;
		}
		
		public function set hScrollSkin(val:String):void
		{
			if(_hScrollSkin	!= val)
			{
				_hScrollSkin		=	val;
				if(null		!=		_nxList)	
				{
					_nxList.hScrollSkin=val;
				}
			}
		}
		
		public function get itemSkin():String
		{
			return _itemSkin;
		}
		
		public function set itemSkin(value:String):void
		{
			if(_itemSkin	!= value)
			{
				_itemSkin		=	value;
				
				if(null		!=		_nxList)	
				{
					_nxList.itemSkin = value;
				}
			}
		}
		
		protected function addListener():void
		{
			_titleContainer.addEventListener(MouseEvent.CLICK,showOrHideList);
			_nxList.addEventListener(NxListEvent.LIST_CLICK,updataTitle);
		}
		
	
		protected function showOrHideList(e:Event):void
		{
			if(_listContainer.visible){
				hideList();
			}else{
				showList();
			}
		}
		
		private function hideList():void
		{
			_listContainer.mouseChildren = _listContainer.mouseEnabled = _listContainer.visible = false;
			if(StageInfo.stage)
			{
				StageInfo.stage.removeEventListener(MouseEvent.CLICK, onHideList);
			}
		}
		
		protected function showList():void
		{
			_listContainer.mouseChildren = _listContainer.mouseEnabled = _listContainer.visible = true;
			StageInfo.stage.addEventListener(MouseEvent.CLICK, onHideList);
		}
		
		private function onHideList(e:MouseEvent):void
		{
			if(_titleItem.contains(e.target as DisplayObject)||e.target.parent.parent is NxScrollBar)return;
			hideList();
		}
		
		private function updataTitle(e:NxListEvent):void
		{
			_titleItem.data = e.itemData;
			
			var nxComboBoxEvt:NxComboBoxEvent = new NxComboBoxEvent(NxComboBoxEvent.NXCOMBOBOX_CLICK,getSelecedItem());
			dispatchEvent(nxComboBoxEvt);
		}
		
		public function get title():String
		{
			return _titleItem.data.label;
		}
		public function set title(str:String):void
		{
			_titleItem.txtTitle=str;
		}
		/**
		 * 获得下标对于的item 
		 * @param $index
		 * @return 
		 * 
		 */
		public function getItem($index:int):Object
		{
			return _dataList[$index];
		}
		
		/**
		 * 得到选择的item 
		 * @return 
		 * 
		 */
		public function getSelecedItem():Object
		{
			return _titleItem.data;
		}
		
		/**
		 *	清空 
		 * 
		 */
		public function clear():void
		{
			_dataList = [];
			_titleItem.data = null;
			_nxList.clearList();
		}
		
		/**
		 * 添加1行记录 
		 * @param $item  data格式:{label:"测试0",data:""} 
		 * 
		 */
		public function addItem(item:Object):void{
			_dataList.push(item);
			_nxList.addItem(item);
			
			initTitle(0);
			
//			if(_dataList.length < showCount)
//			{
//				_nxList.showCount = _dataList.length;
//			}
		}
		
		/**
		 * 设置显示的内容 
		 * @param index
		 * 
		 */
		public function setSelected(index:int):void
		{
			if(index < 0 )return;
			initTitle(index);
		}
		
		public function get dataProvider():Array
		{
			return _nxList.dataProvider;
		}
		
		public function set dataProvider(value:Array):void
		{
			_dataList = value;
			_nxList.dataProvider = value;
			
			initTitle(0);
			
			if(_dataList.length < showCount)
			{
				_nxList.showCount = _dataList.length;
			}else if(_nxList.showCount == 0)
			{
				_nxList.showCount = showCount;
			}
		}
		
		public function get showCount():uint
		{
			return _showCount;
		}
		
		public function set showCount(value:uint):void
		{
			_showCount = value;
			
			if(_nxList != null){
				_nxList.showCount = value;
			}
		}
		
		public function get skin():String
		{
			return _skin;
		}
		
		public function set skin(value:String):void
		{
			_skin = value;
			
			draw();
		}
		
		
		/**
		 *	不支持右对齐 
		 * @param value
		 * 
		 */
		public function set align(value:String):void
		{
			_align = value;
			
			if(_nxList !=null){
				_nxList.itemAlign = value;
				_titleItem.align = value;
			}
		}
		
		public function get align():String
		{
			return _align;
		}

		override public function set width(value:Number):void
		{
			_width = value;
		
			draw();
		}
		
		override public function get width():Number
		{
			return _width;
		}

		override public function set height(value:Number):void
		{
			_height = value;
			draw();
		}
		
		override public function get height():Number
		{
			return _height;	
		}
		
		private function clearAll(container : Sprite):void
		{
			while(container.numChildren > 0){
				container.removeChildAt(0);
			}
		}

		/**
		 *	下拉按钮宽度 
		 */
		public function get downBtnWidth():int
		{
			return _downBtnWidth;
		}

		/**
		 * @private
		 */
		public function set downBtnWidth(value:int):void
		{
			_downBtnWidth = value;
			if(_titleItem != null){
				_titleItem.downBtnWidth = _downBtnWidth;
			}
		}
		
		/**
		 * 设置下拉列表的默认颜色。
		 * */
		public function get defaultTextColor():uint
		{
			return	_defaultTxtColor;
		}
		
		public function set defaultTextColor(val:uint):void
		{
			if(_defaultTxtColor!=val)
			{
				_defaultTxtColor=val;
				
				if(_nxList)
				{
					_nxList.defaultTxtColor = val;
					
					_nxList.dataProvider = _dataList;
				}
			}
		}
	}
}